package main.java.com.lee.juc;

import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicReference;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;

/**
 * @Description TODO
 * @Author winston
 * @DateTime 2022/1/18
 */
public class SelfCycleReentrantLockDemo implements Lock {

    static AtomicReference<Thread> owner = new AtomicReference<Thread>();

    private int count;
    @Override
    public void lock() {
        Thread thread = Thread.currentThread();
        if (owner.get().equals(thread)) {
            count++;
        }
        while (owner.compareAndSet(null, thread)) {
            count++;
            Thread.yield();
        }
    }



    @Override
    public void unlock(){
        Thread thread = Thread.currentThread();
        if (owner.get().equals(thread)) {
            count--;
            if (count == 0) {
                owner.set(null);
            }
        }
    }

    @Override
    public Condition newCondition() {
        return null;
    }
    @Override
    public void lockInterruptibly() throws InterruptedException {

    }

    @Override
    public boolean tryLock() {
        return false;
    }

    @Override
    public boolean tryLock(long time, TimeUnit unit) throws InterruptedException {
        return false;
    }

}
